/*
 * Copyright 2012-2014 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jqd.framework.core.hessian.biz.action.support;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jqd.framework.core.exception.BusinessServiceException;
import com.jqd.framework.core.hessian.model.ServiceRequest;
import com.jqd.framework.core.hessian.model.ServiceResponse;

/**
 * 
 * 记录每个action方法执行花费的时间的拦截器
 * 
 * @author liaozhicheng.cn@gmail.com
 * @date 2012-6-11
 * @since 1.0
 */
public class TimeLogActionHandlerInterceptor extends AbstractActionHandlerInterceptor {
	
	private static final Log log = LogFactory.getLog(TimeLogActionHandlerInterceptor.class);
	
	public static final String _START_TIME = "_START_TIME";
	
	
	@Override
	public void preHandle(ServiceRequest serviceRequest, Object handler)
			throws BusinessServiceException {
		
		long startTime = System.currentTimeMillis();
		serviceRequest.setParameter(_START_TIME, startTime);
	}
	
	
	@Override
	public void postHandle(ServiceRequest serviceRequest, ServiceResponse serviceResponse,
			Object handler) throws BusinessServiceException {
		
		long endTime = System.currentTimeMillis();
		long elapsedTime = endTime - (Long) serviceRequest.getParameter(_START_TIME);
		
		if (log.isInfoEnabled()) {
			log.info(new StringBuilder().append("[action handler : ").append(handler.getClass())
					.append(" , ServiceRequest 'REQUESTED_ACTION_ID' : ")
					.append(serviceRequest.getRequestedActionID())
					.append(" , execute total time : ").append(elapsedTime).append(" ms]")
					.toString());
		}
	}
	
}
